package com.wenhui.schoolyardinfo.mine.ui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.DecelerateInterpolator;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.base.BaseActivity;
import com.wenhui.schoolyardinfo.callback.RcvOnItemViewClickListener;
import com.wenhui.schoolyardinfo.callback.RcvOnLongItemViewClickListener;
import com.wenhui.schoolyardinfo.customview.CommonPopupWindow;
import com.wenhui.schoolyardinfo.customview.RecyclerViewDivider;
import com.wenhui.schoolyardinfo.firstpage.adapter.BaseRecyclerAdapter;
import com.wenhui.schoolyardinfo.firstpage.bean.PhotoBean;
import com.wenhui.schoolyardinfo.firstpage.holder.SmartViewContentHolder;
import com.wenhui.schoolyardinfo.manager.LogManager;
import com.wenhui.schoolyardinfo.manager.PopupWindowManager;
import com.wenhui.schoolyardinfo.manager.ScreenManager;
import com.wenhui.schoolyardinfo.mine.adapter.AlbumAdapter;
import com.wenhui.schoolyardinfo.mine.adapter.AlbumPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/7/6 11:19
 * introduce :
 */

public class AlbumActivity extends BaseActivity implements RcvOnItemViewClickListener, RcvOnLongItemViewClickListener {

    private static final String TAG = "AlbumActivity";
    @BindView(R.id.layout_album)
    FrameLayout layoutAlbum;
    @BindView(R.id.layout_show_pictures)
    LinearLayout layoutShowPictures;
    @BindView(R.id.imv_back)
    ImageView imvBack;
    @BindView(R.id.layout_back)
    RelativeLayout layoutBack;
    @BindView(R.id.tev_title)
    TextView tevTitle;
    @BindView(R.id.imv_more)
    ImageView imvMore;
    @BindView(R.id.layout_more)
    RelativeLayout layoutMore;
    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.album_recycler_view)
    RecyclerView albumRecyclerView;
    @BindView(R.id.tev_check_all)
    TextView tevCheckAll;
    @BindView(R.id.tev_delete)
    TextView tevDelete;
    @BindView(R.id.photo_view)
    PhotoView photoView;

    private List<PhotoBean> photoBeanList;
    private boolean isEditAlbum;
    private AlbumAdapter albumAdapter;
    private GridLayoutManager gridLayoutManager;

    //Popwindow
    private CommonPopupWindow window;
    private RecyclerView moreRecyclerView;
    private LinearLayoutManager layoutManager;
    private RelativeLayout layoutAngleLabelTop;
    private RelativeLayout layoutAngleLabelBottom;
    private BaseRecyclerAdapter<String> baseRecyclerAdapter;
    private List<String> stringList;

    // Hold a reference to the current animator,
    // so that it can be canceled mid-way.
    private Animator currentAnimator;

    // The system "short" animation time duration, in milliseconds. This
    // duration is ideal for subtle animations or animations that occur
    // very frequently.
    private int shortAnimationDuration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_album);
        ButterKnife.bind(this);


        initData();
        initViews();
    }

    @Override
    protected void initData() {
        photoBeanList = new ArrayList<>();

        photoBeanList.add(new PhotoBean(0, "校园", "http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ww1.sinaimg.cn/large/0065oQSqly1fu7xueh1gbj30hs0uwtgb.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ww1.sinaimg.cn/large/0065oQSqly1fu7xueh1gbj30hs0uwtgb.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg",false));
                photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ww1.sinaimg.cn/large/0065oQSqly1fu7xueh1gbj30hs0uwtgb.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ww1.sinaimg.cn/large/0065oQSqly1fu7xueh1gbj30hs0uwtgb.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fuh5fsvlqcj30sg10onjk.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ww1.sinaimg.cn/large/0065oQSqly1fu7xueh1gbj30hs0uwtgb.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg",false));
                photoBeanList.add(new PhotoBean(0, "校园", "https://ww1.sinaimg.cn/large/0065oQSqly1fu7xueh1gbj30hs0uwtgb.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "http://ww1.sinaimg.cn/large/0065oQSqly1fsfq1k9cb5j30sg0y7q61.jpg",false));
                photoBeanList.add(new PhotoBean(0, "校园", "https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/" +
                        "sign=5b4f6ad1a74bd11310c0bf603bc6cf6a/023b5bb5c9ea15cea5096eabbc003af33b87b2a9.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/" +
                "sign=5b4f6ad1a74bd11310c0bf603bc6cf6a/023b5bb5c9ea15cea5096eabbc003af33b87b2a9.jpg", false));
        photoBeanList.add(new PhotoBean(0, "校园", "https://ws1.sinaimg.cn/large/0065oQSqly1fw0vdlg6xcj30j60mzdk7.jpg", false));


        stringList = new ArrayList<>();
        stringList.add("编辑相册");
        stringList.add("分享");
        stringList.add("上传照片");
        isEditAlbum = false;

        shortAnimationDuration = 500;
    }

    @Override
    protected void initViews() {
        setTopBar(topbar);

        initAdapter();
        initPopupWindow();
    }

    private void initAdapter() {
        gridLayoutManager = new GridLayoutManager(this, 3);
        albumRecyclerView.setLayoutManager(gridLayoutManager);
        albumRecyclerView.setItemAnimator(new DefaultItemAnimator());

        albumAdapter = new AlbumAdapter(this);
        albumRecyclerView.setAdapter(albumAdapter);
        albumAdapter.setRcvOnItemViewClickListener(this);
        albumAdapter.setRcvOnLongItemViewClickListener(this);
        albumAdapter.clearData();
        albumAdapter.addAllData(photoBeanList, false);
    }

    private void initPopupWindow() {
        // create popup window
        window = new CommonPopupWindow(this, R.layout.popup_window_public_good, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
            @Override
            protected void initViews() {
                View view = getContentView();
                layoutManager = new LinearLayoutManager(AlbumActivity.this);
                layoutManager.setOrientation(OrientationHelper.VERTICAL);

                moreRecyclerView = view.findViewById(R.id.more_recycler_view);
                layoutAngleLabelTop = view.findViewById(R.id.layout_angle_label_top);
                layoutAngleLabelBottom = view.findViewById(R.id.layout_angle_label_bottom);
                moreRecyclerView.setItemAnimator(new DefaultItemAnimator());
                moreRecyclerView.setLayoutManager(layoutManager);
                moreRecyclerView.addItemDecoration(new RecyclerViewDivider(
                        AlbumActivity.this,
                        LinearLayoutManager.VERTICAL,
                        ScreenManager.dipTopx(AlbumActivity.this, 1),
                        getResources().getColor(R.color.colorGrayB)));
                baseRecyclerAdapter = new BaseRecyclerAdapter<String>(R.layout.item_popup_window_recommend, 1) {
                    @Override
                    protected void onBindViewHolder(RecyclerView.ViewHolder holder, String model, int position) {
                        if (holder instanceof SmartViewContentHolder) {
                            SmartViewContentHolder smartViewContentHolder = (SmartViewContentHolder) holder;
                            smartViewContentHolder.text(R.id.tev_concern, model);

                            View viewConcern = smartViewContentHolder.getView(R.id.view_concern);
                            if (position == stringList.size() - 1) {
                                viewConcern.setVisibility(View.GONE);
                            }
                        }
                    }
                };

                moreRecyclerView.setAdapter(baseRecyclerAdapter);

                baseRecyclerAdapter.clearData();
                baseRecyclerAdapter.addAllData(stringList);
            }

            @Override
            protected void initEvent() {
                baseRecyclerAdapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        switch (position) {
                            case 0:
                                showToast("编辑相册");
                                albumAdapter.clearData();
                                albumAdapter.addAllData(photoBeanList, true);
                                tevDelete.setVisibility(View.VISIBLE);
                                isEditAlbum = true;
                                break;
                            case 1:
                                showToast("点击了privateLetter");
                                break;
                            case 2:
                                showToast("点击了collect");
                                break;
                        }
                        window.getPopupWindow().dismiss();
                    }
                });
            }

            @Override
            protected void initWindow() {
                super.initWindow();
                PopupWindow instance = getPopupWindow();
                instance.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        WindowManager.LayoutParams lp = getWindow().getAttributes();
                        lp.alpha = 1.0f;
                        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                        getWindow().setAttributes(lp);
                    }
                });
            }
        };
    }

    private void showPopupWindow(View anchorView) {
        PopupWindow mPopupWindow = window.getPopupWindow();
        mPopupWindow.setAnimationStyle(R.style.animTranslate);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
        // 设置点击窗口外边窗口消失
        mPopupWindow.setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        mPopupWindow.setFocusable(true);
        // 设置好参数之后再show
        int windowPos[] = PopupWindowManager.calculatePopWindowPos(anchorView, window.getContentView());
        int xOff = 20; // 可以自己调整偏移
        windowPos[0] -= xOff;

        //是否需要向上弹出/向下弹出显示
        boolean isNeedShowUp = PopupWindowManager.isNeedShowUp(anchorView, window.getContentView());
        if (isNeedShowUp) {
            layoutAngleLabelTop.setVisibility(View.GONE);
            layoutAngleLabelBottom.setVisibility(View.VISIBLE);
        } else {
            layoutAngleLabelTop.setVisibility(View.VISIBLE);
            layoutAngleLabelBottom.setVisibility(View.GONE);
        }
        // 设置popupWindow的显示位置
        mPopupWindow.showAtLocation(anchorView, Gravity.TOP | Gravity.START, windowPos[0], windowPos[1]);

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.3f;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getWindow().setAttributes(lp);
    }

    /**
     * @param thumbView
     * @param photoUrl
     */
    private void zoomImageFromThumb(final View thumbView, String photoUrl, final int position) {
        // If there's an animation in progress, cancel it
        // immediately and proceed with this one.
        if (currentAnimator != null) {
            currentAnimator.cancel();
        }

        // Load the high-resolution "zoomed-in" image.
//        final ImageView photoView = (ImageView) findViewById(
//                R.id.expanded_image);
        Glide.with(this).load(photoUrl).into(photoView);

        // Calculate the starting and ending bounds for the zoomed-in image.
        // This step involves lots of math. Yay, math.
        final Rect startBounds = new Rect();
        final Rect finalBounds = new Rect();
        final Point globalOffset = new Point();

        // The start bounds are the global visible rectangle of the thumbnail,
        // and the final bounds are the global visible rectangle of the container
        // view. Also set the container view's offset as the origin for the
        // bounds, since that's the origin for the positioning animation
        // properties (X, Y).
        thumbView.getGlobalVisibleRect(startBounds);
        layoutAlbum
                .getGlobalVisibleRect(finalBounds, globalOffset);
        startBounds.offset(-globalOffset.x, -globalOffset.y);
        finalBounds.offset(-globalOffset.x, -globalOffset.y);

        // Adjust the start bounds to be the same aspect ratio as the final
        // bounds using the "center crop" technique. This prevents undesirable
        // stretching during the animation. Also calculate the start scaling
        // factor (the end scaling factor is always 1.0).
        float startScale;
        if ((float) finalBounds.width() / finalBounds.height()
                > (float) startBounds.width() / startBounds.height()) {
            // Extend start bounds horizontally
            startScale = (float) startBounds.height() / finalBounds.height();
            float startWidth = startScale * finalBounds.width();
            float deltaWidth = (startWidth - startBounds.width()) / 2;
            startBounds.left -= deltaWidth;
            startBounds.right += deltaWidth;
        } else {
            // Extend start bounds vertically
            startScale = (float) startBounds.width() / finalBounds.width();
            float startHeight = startScale * finalBounds.height();
            float deltaHeight = (startHeight - startBounds.height()) / 2;
            startBounds.top -= deltaHeight;
            startBounds.bottom += deltaHeight;
        }

        // Hide the thumbnail and show the zoomed-in view. When the animation
        // begins, it will position the zoomed-in view in the place of the
        // thumbnail.
        thumbView.setAlpha(0f);
        photoView.setVisibility(View.VISIBLE);

        // Set the pivot point for SCALE_X and SCALE_Y transformations
        // to the top-left corner of the zoomed-in view (the default
        // is the center of the view).
        photoView.setPivotX(0f);
        photoView.setPivotY(0f);

        // Construct and run the parallel animation of the four translation and
        // scale properties (X, Y, SCALE_X, and SCALE_Y).
        AnimatorSet set = new AnimatorSet();
        set
                .play(ObjectAnimator.ofFloat(photoView, View.X,
                        startBounds.left, finalBounds.left))
                .with(ObjectAnimator.ofFloat(photoView, View.Y,
                        startBounds.top, finalBounds.top))
                .with(ObjectAnimator.ofFloat(photoView, View.SCALE_X,
                        startScale, 1f))
                .with(ObjectAnimator.ofFloat(photoView,
                        View.SCALE_Y, startScale, 1f));
        set.setDuration(shortAnimationDuration);
        set.setInterpolator(new DecelerateInterpolator());
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                currentAnimator = null;

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        photoView.setVisibility(View.GONE);
                        layoutShowPictures.setVisibility(View.GONE);
                        tevDelete.setVisibility(View.GONE);

                        Intent intent = new Intent(AlbumActivity.this, ShowPicturesActivity.class);
                        Bundle bundle = new Bundle();
                        ArrayList<String> photoUrlList = new ArrayList<>();
                        for (int i = 0; i < photoBeanList.size(); i++) {
                            photoUrlList.add(photoBeanList.get(i).getPhotoUrl());
                        }
                        bundle.putInt("currentPosition", position);
                        bundle.putStringArrayList("photoUrlList", photoUrlList);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        overridePendingTransition(R.anim.translate_out, R.anim.translate_in);
                    }
                });

            }

            @Override
            public void onAnimationCancel(Animator animation) {
                currentAnimator = null;
            }
        });
        set.start();
        currentAnimator = set;

        // Upon clicking the zoomed-in image, it should zoom back down
        // to the original bounds and show the thumbnail instead of
        // the expanded image.
        final float startScaleFinal = startScale;
        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (currentAnimator != null) {
                    currentAnimator.cancel();
                }

                // Animate the four positioning/sizing properties in parallel,
                // back to their original values.
                AnimatorSet set = new AnimatorSet();
                set.play(ObjectAnimator
                        .ofFloat(photoView, View.X, startBounds.left))
                        .with(ObjectAnimator
                                .ofFloat(photoView,
                                        View.Y, startBounds.top))
                        .with(ObjectAnimator
                                .ofFloat(photoView,
                                        View.SCALE_X, startScaleFinal))
                        .with(ObjectAnimator
                                .ofFloat(photoView,
                                        View.SCALE_Y, startScaleFinal));
                set.setDuration(shortAnimationDuration);
                set.setInterpolator(new DecelerateInterpolator());
                set.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        thumbView.setAlpha(1f);
                        photoView.setVisibility(View.GONE);
                        currentAnimator = null;
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                        thumbView.setAlpha(1f);
                        photoView.setVisibility(View.GONE);
                        currentAnimator = null;
                    }
                });
                set.start();
                currentAnimator = set;
            }
        });
    }

    @Override
    public void onItemClickListener(int position, View view) {
        switch (view.getId()) {
            case R.id.imv_photo:
                showToast("imv_photo");

                Intent intent = new Intent(AlbumActivity.this, ShowPicturesActivity.class);
                Bundle bundle = new Bundle();
                ArrayList<String> photoUrlList = new ArrayList<>();
                for (int i = 0; i < photoBeanList.size(); i++) {
                    photoUrlList.add(photoBeanList.get(i).getPhotoUrl());
                }
                bundle.putInt("currentPosition", position);
                bundle.putStringArrayList("photoUrlList", photoUrlList);
                intent.putExtras(bundle);
                startActivity(intent);
                overridePendingTransition(R.anim.translate_out, R.anim.translate_in);
//                zoomImageFromThumb(view, photoBeanList.get(position).getPhotoUrl(),position);

                break;
            case R.id.layout_edit_album:
                if (photoBeanList.get(position).isChecked()) {
                    photoBeanList.get(position).setChecked(false);
                    albumAdapter.clearData();
                    albumAdapter.addAllData(photoBeanList, true);
                } else {
                    photoBeanList.get(position).setChecked(true);
                    albumAdapter.clearData();
                    albumAdapter.addAllData(photoBeanList, true);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onLongItemClickListener(int position, View view) {
        switch (view.getId()) {
            case R.id.imv_photo:
                showToast("long_imv_photo");
                albumAdapter.clearData();
                albumAdapter.addAllData(photoBeanList, true);
                tevDelete.setVisibility(View.VISIBLE);
                isEditAlbum = true;
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isEditAlbum) {
                albumAdapter.clearData();
                albumAdapter.addAllData(photoBeanList, false);
                tevDelete.setVisibility(View.GONE);
                isEditAlbum = !isEditAlbum;
                return false;
            } else {
                finish();
                return false;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @OnClick({R.id.layout_back, R.id.layout_more, R.id.tev_check_all, R.id.tev_delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                if (isEditAlbum) {
                    albumAdapter.clearData();
                    albumAdapter.addAllData(photoBeanList, false);
                    tevDelete.setVisibility(View.GONE);
                    isEditAlbum = !isEditAlbum;
                } else {
                    finish();
                }
                break;
            case R.id.layout_more:
                showPopupWindow(view);
                break;
            case R.id.tev_check_all:
                for (int i = 0; i < photoBeanList.size(); i++) {
                    photoBeanList.get(i).setChecked(true);
                }

                albumAdapter.clearData();
                albumAdapter.addAllData(photoBeanList, true);
                tevDelete.setVisibility(View.VISIBLE);
                break;
            case R.id.tev_delete:
//                for (int i = 0; i < photoBeanList.size(); i++) {//这里正向删除，list会错位
//                    if (photoBeanList.get(i).isChecked()) {
//                        photoBeanList.remove(i);
//                        LogManager.i(TAG, "photoBeanList.remove*****" + i);
//                    }
//                }

                //逆向思维（这里必须反向删除，由于正向删除list中间部分，会造成list错位，所以必须用反向删除）
                for (int i = photoBeanList.size() - 1; i >= 0; i--) {
                    if (photoBeanList.get(i).isChecked()) {
                        photoBeanList.remove(i);
                        LogManager.i(TAG, "photoBeanList.remove*****" + i);
                    }
                }
                albumAdapter.clearData();
                albumAdapter.addAllData(photoBeanList, false);
                tevDelete.setVisibility(View.GONE);
                break;
        }
    }
}
